package com.itheima.web.controller.company;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.common.utils.UtilFuns;
import com.itheima.domain.system.Company;
import com.itheima.service.ICompanyService;
import com.itheima.web.controller.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

    @Reference
    private ICompanyService companyService;

    @RequiresPermissions("企业管理")
    @RequestMapping(value = "/list",name = "分页查询企业列表")
    public String list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size) {

        PageInfo pageInfo = companyService.findByPageInfo(page, size);

        request.setAttribute("page", pageInfo);

        return "company/company-list";

    }

    /**
     * 传统分页分页查询企业列表
     * @param page
     * @param size
     * @param request
     * @return
     *//*
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "4") int size,HttpServletRequest request){

        request.setAttribute("page",companyService.findPage(page, size));

        return "company/company-list";

    }
*/
    /**
     * 企业管理控制器
     * @param
     * @return
     */
   /* @RequestMapping("/list")
    public String list(HttpServletRequest request){

        //调用业务
        List<Company> companyList = companyService.findAll();
        //存入请求域
        request.setAttribute("list",companyList);

        //转发到页面
        return "company/company-list";
    }*/

    @RequestMapping(value = "/toAdd",name = "前往新建企业页面")
    public String toAdd() {
        return "company/company-add";
    }

    @RequestMapping(value = "/edit",name = "企业的跟新与保存")
    public String edit(Company company) {
        //StringUtils.isEmpty(company.getId())   判断是否含有id，有就更新，没有就是保存
        if (UtilFuns.isEmpty(company.getId())) {

            //保存
            companyService.save(company);
        } else {

            //更新
            companyService.update(company);
        }
        //重定向跳转页面
        return "redirect:/company/list.do";
    }

    @RequestMapping(value = "/toUpdate" ,name = "请往更新企业页面")
    public String toUpdate(String id, HttpServletRequest request) {

        //根据用户Id查询用户信息进行回显
        Company company = companyService.findById(id);
        //把用户信息存储到域中
        request.setAttribute("company", company);
        //转发到更新页面
        return "company/company-update";
    }

    @RequestMapping(value = "/delete",name = "删除企业用户")
    public String delete(String id) {

        companyService.delete(id);
        //重定向跳转页面
        return "redirect:/company/list.do";
    }
}
